package com.github.livebase.log;

import com.github.livebase.util.base.ObjectsUtil;

/**
 * @ClassName ConsoleLog
 * @description:
 * @author: biao532
 * @create: 2024-02-02 12:08
 * @Version 1.0
 **/
public abstract class AbstractLogger implements Logger {
    public static int LEVEL_TRANCE = 1;
    public static int LEVEL_DEBUG = 2;
    public static int LEVEL_INFO = 3;
    public static int LEVEL_WARN = 4;
    public static int LEVEL_ERROR = 5;
    private static String[] names = new String[] {
            "", "TRANCE", "DEBUG ", "INFO  ", "WARN  ", "ERROR "
    };

    private int level;
    private Formatter formatter;
    protected AbstractLogger(int level, Formatter formatter) {
        this.level = level;
        this.formatter = formatter;
    }
    protected AbstractLogger(int level) {
        this(level, LFormatter.getFormatter());
    }
    protected void setLevel(int level) {
        this.level = level;
    }

    public abstract void println(String tag, String message, String levelName);
    public void println(int level, String tag, Throwable throwable, String format, Object... params) {
        if (!isLoggable(level)) {
            return;
        }
        String msg = formatter.format(throwable, format, params);
        println(tag, msg, names[level]);
    }

    @Override
    public boolean isTrance() {
        return isLoggable(LEVEL_TRANCE);
    }

    @Override
    public void trance(String tag, Throwable throwable, String format, Object... params) {
        println(LEVEL_TRANCE, tag, throwable, format, params);
    }

    @Override
    public boolean isDebug() {
        return isLoggable(LEVEL_DEBUG);
    }

    @Override
    public void debug(String tag, Throwable throwable, String format, Object... params) {
        println(LEVEL_DEBUG, tag, throwable, format, params);
    }

    @Override
    public boolean isInfo() {
        return isLoggable(LEVEL_INFO);
    }

    @Override
    public void info(String tag, Throwable throwable, String format, Object... params) {
        println(LEVEL_INFO, tag, throwable, format, params);
    }

    @Override
    public boolean isWarm() {
        return isLoggable(LEVEL_WARN);
    }

    @Override
    public void warn(String tag, Throwable throwable, String format, Object... params) {
        println(LEVEL_WARN, tag, throwable, format, params);
    }

    @Override
    public boolean isError() {
        return isLoggable(LEVEL_ERROR);
    }

    @Override
    public void error(String tag, Throwable throwable, String format, Object... params) {
        println(LEVEL_ERROR, tag, throwable, format, params);
    }

    private boolean isLoggable(int level) {
        return level >= this.level;
    }
}
